package edu.lingnan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.lingnan.pojo.ShoppingCart;
import edu.lingnan.service.ShoppingCartService;
import edu.lingnan.mapper.ShoppingCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
    implements ShoppingCartService{

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public String  addShoppingCart(ShoppingCart shoppingCart) {
        //判断购物车中是否有该商品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCartByUserIdAndProductId(shoppingCart);
        if (shoppingCartList.isEmpty()) {
            //加入购物车
            shoppingCartMapper.addShoppingCart(shoppingCart);
            return "加入购物车成功";
        } else {
            //修改商品数量
           shoppingCartMapper.increaseProductNumber(shoppingCart);
            return "商品数量更新成功";
        }
    }

    @Override
    public List<ShoppingCart> myShoppingCart(Long userId) {
        return shoppingCartMapper.selectShoppingCartByUserId(userId);
    }

    @Override
    public String deleteMyProduct(ShoppingCart shoppingCart) {
        int row = shoppingCartMapper.deleteMyProduct(shoppingCart);
        if(row == 0){
            return "购物车没有该商品";
        }
        return "删除成功";
    }
}




